package org.vincen.helloworld.stax;

import java.io.FileInputStream;
import java.io.FileNotFoundException;

import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;

public class StaxStreamParser {

	public static void main(String args[]) throws FileNotFoundException, XMLStreamException {
		XMLInputFactory factory = XMLInputFactory.newFactory();
		XMLStreamReader reader = factory.createXMLStreamReader(new FileInputStream(
						"./src/main/java/org/vincen/helloworld/stax/config.xml"));
		int event = reader.getEventType();
		do {
			switch (event) {
			case XMLStreamConstants.START_DOCUMENT: // '7'
				System.out.println("start document");
				break;

			case XMLStreamConstants.START_ELEMENT: // '1'
				System.out.println((new StringBuilder("start element 1 : "))
						.append(reader.getName()).toString());
				break;

			case XMLStreamConstants.CHARACTERS: // '4'
				if (reader.isWhiteSpace())
					System.out.println("white space");
				else
					System.out.println((new StringBuilder("characters 4 : "))
							.append(reader.getText()).toString());
				break;

			case XMLStreamConstants.END_ELEMENT: // '2'
				System.out.println((new StringBuilder("end element 2 : "))
						.append(reader.getName()).toString());
				break;

			case XMLStreamConstants.END_DOCUMENT: // '8'
				System.out.println("end document");
				break;
			}
			if (reader.hasNext())
				event = reader.next();
			else
				return;
		} while (true);
	}
}
